package com.ruoyi.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.InvInventoryTransferDetail;
import com.ruoyi.system.domain.InvReportingLosses;
import com.ruoyi.system.domain.Warehouse;
import com.ruoyi.system.mapper.ReportingLossesMapper;
import com.ruoyi.system.service.IInvInventoryTransferDetailService;
import com.ruoyi.system.service.IReportingLossesService;
import com.ruoyi.system.service.IWarehouseService;
import com.ruoyi.system.util.AbdulEmptyUtils;
import com.ruoyi.system.util.BookingNumberGenerator;
import com.ruoyi.system.util.StoreIdUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 库存报损 服务实现类
 *
 * @author ruoyi
 * @date 2025-06-04
 */
@Service
public class ReportingLossesServiceImpl extends ServiceImpl<ReportingLossesMapper, InvReportingLosses> implements IReportingLossesService {

    @Autowired
    private IWarehouseService warehouseService;

    @Autowired
    private StoreIdUtils storeIdUtils;

    @Autowired
    private IInvInventoryTransferDetailService invInventoryTransferDetailService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean create(InvReportingLosses reportingLosses) {
        //1-生成报损单
        queryValidate(reportingLosses);
        reportingLosses.setReportLossNumber(BookingNumberGenerator.generateBookingNumber());
        reportingLosses.setIsStatus(1);
        reportingLosses.setCreateBy(SecurityUtils.getUsername());
        reportingLosses.setUpdateBy(SecurityUtils.getUsername());
        reportingLosses.setStoreId(storeIdUtils.getStoreId());
        this.save(reportingLosses);
        // 2-增加报损详情
        Long id = reportingLosses.getId();
        List<InvInventoryTransferDetail> detailList = reportingLosses.getDetailList();
        if (AbdulEmptyUtils.isEmpty(detailList)) {
            throw new RuntimeException("报损详情不能为空");
        }
        editCheck(detailList, id);
        return invInventoryTransferDetailService.saveBatch(detailList);
    }

    @Override
    public Boolean edit(InvReportingLosses reportingLosses) {
        if (AbdulEmptyUtils.isEmpty(reportingLosses.getId())) {
            throw new RuntimeException("报损单id不能为空");
        }
        InvReportingLosses transfer = this.getById(reportingLosses.getId());
        if (AbdulEmptyUtils.isEmpty(transfer)) {
            throw new RuntimeException("报损单不存在");
        }
        if (!AbdulEmptyUtils.isEmpty(reportingLosses.getLossesTime())) {
            transfer.setLossesTime(reportingLosses.getLossesTime());
        }
        if (!AbdulEmptyUtils.isEmpty(reportingLosses.getLossesStaff())) {
            transfer.setLossesStaff(reportingLosses.getLossesStaff());
        }
        if (!AbdulEmptyUtils.isEmpty(reportingLosses.getRemark())) {
            transfer.setRemark(reportingLosses.getRemark());
        }
        LambdaQueryWrapper<InvInventoryTransferDetail> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(InvInventoryTransferDetail::getTransferId, reportingLosses.getId());
        wrapper.eq(InvInventoryTransferDetail::getDetailType, 2);
        invInventoryTransferDetailService.remove(wrapper);
        if (AbdulEmptyUtils.isEmpty(reportingLosses.getDetailList())) {
            throw new RuntimeException("报损详情不能为空");
        }
        editCheck(reportingLosses.getDetailList(), reportingLosses.getId());
        return invInventoryTransferDetailService.saveBatch(reportingLosses.getDetailList());
    }


    /**
     * 查询效验
     */
    public void queryValidate(InvReportingLosses reportingLosses) {
        if (AbdulEmptyUtils.isEmpty(reportingLosses.getWarehouseId())) {
            throw new RuntimeException("报损仓库不能为空");
        }
        Warehouse warehouse = warehouseService.getById(reportingLosses.getWarehouseId());
        if (AbdulEmptyUtils.isEmpty(warehouse)) {
            throw new RuntimeException("报损仓库不存在");
        }
        if (AbdulEmptyUtils.isEmpty(reportingLosses.getLossesTime())) {
            throw new RuntimeException("报损时间不能为空");
        }
        if (AbdulEmptyUtils.isEmpty(reportingLosses.getLossesStaff())) {
            throw new RuntimeException("报损员工不能为空");
        }
        if (AbdulEmptyUtils.isEmpty(reportingLosses.getStoreBusinessId())) {
            throw new RuntimeException("报损类型不能为空");
        }
    }

    public void editCheck(List<InvInventoryTransferDetail> detailList, Long id) {
        for (InvInventoryTransferDetail detail : detailList) {
            detail.setTransferId(id);
            detail.setStoreId(storeIdUtils.getStoreId());
            detail.setCreateBy(SecurityUtils.getUsername());
            detail.setDetailType(2);
            if (AbdulEmptyUtils.isEmpty(detail.getProductManagementId())) {
                throw new RuntimeException("商品id不能为空");
            }
            if (AbdulEmptyUtils.isEmpty(detail.getStockPrice())) {
                throw new RuntimeException("商品单价不能为空");
            }
            if (AbdulEmptyUtils.isEmpty(detail.getStockQuantity()) || detail.getStockQuantity() <= 0) {
                throw new RuntimeException("库存数量为空或者库存数量小于0");
            }
            if (AbdulEmptyUtils.isEmpty(detail.getTransferQuantity()) || detail.getTransferQuantity() <= 0) {
                throw new RuntimeException("报损数量为空或者报损数量小于0");
            }
            if (detail.getStockQuantity() < detail.getTransferQuantity()) {
                throw new RuntimeException("库存数量不能小于报损数量");
            }
        }
    }
}
